﻿#ifndef PCPUDPCTRL_H
#define PCPUDPCTRL_H

#include <QThread>
#include <QUdpSocket>

class QUdpSocket;
class UdpSocket : public QObject
{
    Q_OBJECT
public:
    explicit UdpSocket(QObject *parent = nullptr);

    QList<QString> getClientList() { return m_clientObjList; }

    void cleanup();
    bool listen(const QHostAddress &ip, int port);
    bool isListening() { return m_socket->isValid(); }
    bool stopListen();
    void deleteClient(const QString &clientStr);

    void resetCNT() { m_sendMsgLenCnt = m_revMsgLenCnt = 0; }

    void sendMsgToClient(const QString &ip, int port, const QByteArray &msg);
    void sendMsgToClient(const QString &clientStr, const QByteArray &msg);

public:
    void sendData(const QByteArray &buffer, const QString &ip, int port);

signals:
    void sigClientUpdate();
    void sigReadMsg(const QString &ip, const QByteArray& msg);
    void sigCNTUpdate(int send_len, int rev_len);

    void sigSystemLog(const QString &log_msg);

private slots:
    void onReadyRead();        //UDP Socket接收消息

private:
    int m_sendMsgLenCnt;    ///< 发送的字符长度计数
    int m_revMsgLenCnt;     ///< 收到的字符长度计数

    QUdpSocket *m_socket;
    QList<QString> m_clientObjList;

};

#endif // PCPUDPCTRL_H
